<template>
  <div style="height: 100vh;overflow: hidden;position: relative">

    <!--滑块验证-->
    <el-card class="cover" v-if="loginAdmin.id">
      <slide-verify :l="42"
                    :r="10"
                    :w="310"
                    :h="155"
                    :accuracy="5"
                    slider-text="向右滑动"
                    @success="onSuccess"
                    @fail="onFail"
                    @refresh="onRefresh"
      ></slide-verify>
    </el-card>

    <div
        style="width: 500px; height:400px; background-color:white;border-radius: 10px;margin: 150px auto;padding: 50px">
      <el-form :model="admin" :rules="rules" ref="loginForm">

        <div style="margin: 30px;text-align: center;font-size: 30px;font-weight: bold;color: #2483e0">登 录</div>

        <el-form-item prop="username">
          <el-input placeholder="请输入账号" prefix-icon="el-icon-user" size="medium"
                    v-model="admin.username"></el-input>
        </el-form-item>

        <el-form-item prop="password">
          <el-input placeholder="请输入密码" show-password prefix-icon="el-icon-lock" size="medium"
                    v-model="admin.password"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button style="width: 100%;" size="medium" type="primary" @click="login">登录</el-button>
        </el-form-item>

      </el-form>
    </div>
  </div>
</template>

<script>
import request from "@/utils/request";
import Cookies from 'js-cookie';

export default {
  name: "Login",
  data() {
    return {
      admin: {},
      loginAdmin: {},
      rules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur'}
        ],
      }
    }
  },
  methods: {
    login() {
      this.$refs['loginForm'].validate((valid) => {
        if (valid) {
          request.post('/admin/login', this.admin).then(res => {
            if (res.code === '200') {
              this.loginAdmin = res.data  //滑块组件出现
              // this.$message.success("登陆成功")
              // if (res.data !== null) {      //登陆成功之后先存储数据(因为路由守卫会根据cookie是否有admin数据来判断是否跳到登录页)，再跳转到主页
              //   Cookies.set('admin', JSON.stringify(res.data))   //存储当前登陆的用户数据
              // }
              // this.$router.push('/')   //跳转到主页
            } else if (res.code === '402') {    //返回402，表示用户被禁用
              this.$message.error("当前用户已被禁用，请联系管理员")
            } else {
              this.$message.error("用户名或密码错误")
            }
          })
        }
      })
    },
    onSuccess() { // 滑块验证通过之后触发的
      Cookies.set('admin', JSON.stringify(this.loginAdmin))
      this.$message.success("登录成功")
      this.$router.push('/')
    },
    onFail() {
      this.$message.warning("请重新验证")
      console.log('onFail')
    },
    onRefresh() {
      console.log('refresh')
    }
  }
}
</script>
<style>
.cover {
  /*滑块居中并置顶*/
  width: fit-content;
  background-color: white;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  z-index: 1000;
}
</style>